<template>
  <div>
    <!-- 定义面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 定义卡片 -->
    <el-card class="box-card">

      <!-- 定义第一行数据 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryItemInfo.query" clearable @clear="getOrderList">
            <el-button slot="append" icon="el-icon-search" @click="getOrderList"></el-button>
          </el-input>
        </el-col>
      </el-row>

      <!-- 获取商品列表数据 -->
      <el-table :data="orderList" style="width: 100%" stripe border>
        <el-table-column prop="serialnumber" label="订单编号" width="300px">
        </el-table-column>
        <el-table-column prop="productName" label="商品标题" width="100px">
        </el-table-column>
        <el-table-column prop="productFileName" label="商品图片" width="120px">
          <template slot-scope="scope">
            <el-image :src="'http://localhost:8011/images/'+scope.row.productFileName"
                      style="height: 100px;width: 100px"/>
          </template>
        </el-table-column>
        <el-table-column prop="productQuantity" label="数量" width="80px">
        </el-table-column>
        <!-- 使用过滤器修改时间 在main.js中定义过滤器 -->
        <el-table-column prop="cost" label="总价(元)" width="80px">
        </el-table-column>
        <el-table-column prop="loginName" label="购买者" width="100px">
        </el-table-column>
        <el-table-column prop="userAddress" label="地址" width="200px">
        </el-table-column>
        <el-table-column prop="status" label="状态" width="80px">
          <template slot-scope="scope">
<!--            <el-switch v-model="scope.row.status" active-color="#13ce66" inactive-color="#ff4949"-->
<!--                      @change="updateStatus(scope.row)"></el-switch>-->
            <el-button v-if="scope.row.status == 1" @click="updateStatus(scope.row)">发货</el-button>
            <span v-if="scope.row.status == 2">已发货</span>
            <span v-if="scope.row.status == 3">完成</span>
          </template>
        </el-table-column>
<!--         <el-table-column prop="updated" label="更新时间" width="180px" :formatter="formatDate">-->
<!--         </el-table-column> -->
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="updateItemBtn(scope.row)">修改</el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteItemBtn(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 定义分页插件-->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                     :current-page="queryItemInfo.pageNum" :page-sizes="[1, 5, 10, 20]"
                     :page-size="queryItemInfo.pageSize"
                     layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>

    </el-card>
    <!-- 5. 定义修改商品的对话框 -->
    <el-dialog title="编辑订单" :visible.sync="updateDialogVisible"
               @close="closeUpdateDialog" width="65%">
      <el-form :model="updateOrdersForm"
               label-width="100px" class="demo-ruleForm">
        <el-form-item label="购买者" prop="name">
          <el-input v-model="updateOrdersForm.loginName"></el-input>
        </el-form-item>
        <el-form-item label="地址" prop="description">
          <el-input v-model="updateOrdersForm.userAddress"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
          <el-button @click="updateDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="updateProductBtn">确 定</el-button>
        </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 1.定义订单列表信息
      orderList: [],
      // 2.定义分页对象
      queryItemInfo: {
        query: '', // 定义查询参数
        pageNum: 1,
        pageSize: 10
      },
      total: 0,
      updateDialogVisible: false,
      updateOrdersForm: { // 设置 修改的对象
        id: '',
        loginName: '',
        userAddress: '',

      }
    }
  },
  created() {
    // 1.获取商品列表数据
    this.getOrderList()
  },
  methods: {
    // 实现商品信息分页查询
    async getOrderList() {
      const {
        data: result
      } = await this.$http.get('/orders/getOrderList', {
        params: this.queryItemInfo
      })
      if (result.status !== 200) return this.$message.error('商品列表查询失败')
      this.orderList = result.data.rows
      this.total = result.data.total
    },
    //修改商品信息
    async updateProductBtn() {
      const {data: result} = await this.$http.put('/orders/updateProduct', this.updateOrdersForm)
      if (result.status !== 200) return this.$message.error('修改订单信息失败')
      this.$message.success('更新状态成功')
      this.updateDialogVisible = false
      this.getOrderList();
    },
    closeUpdateDialog() {
    },
    // 通过JS格式化时间
    // formatDate (row, column, cellValue, index) {
    //   const date = new Date(cellValue)
    //   const year = date.getFullYear()
    //   const month = (date.getMonth() + 1 + '').padStart(2, '0')
    //   const day = (date.getDate() + '').padStart(2, '0')
    //   const HH = (date.getHours() + '').padStart(2, '0')
    //   const MM = (date.getMinutes() + '').padStart(2, '0')
    //   const SS = (date.getSeconds() + '').padStart(2, '0')
    //   return year + '-' + month + '-' + day + ' ' + HH + ':' + MM + ':' + SS
    // },
    // 条数变化时 调用
    handleSizeChange(size) {
      this.queryItemInfo.pageSize = size
      this.getOrderList()
    },
    //页码值变化时 调用
    handleCurrentChange(current) {
      this.queryItemInfo.pageNum = current
      this.getOrderList()
    },
    async updateStatus (item) {
      const {
        data: result
      } = await this.$http.put('/orders/updateItemStatus', {id: item.id})
      if (result.status !== 200) return this.$message.error('发货失败')
      this.$message.success('发货成功')
      this.getOrderList();
    },
    async deleteItemBtn(item) {
      // 消息确认框
      this.$confirm('此操作将永久删除该订单, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        // 根据id删除数据
        const {
          data: result
        } = await this.$http.delete('/orders/'+item.id)
        if (result.status !== 200) return this.$message.error('订单删除失败')
        this.$message.success('订单删除成功')
        // 重新获取列表信息
        this.getOrderList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // // 转向到商品新增页面
    // toAddItem() {
    //   this.$router.push('/item/addItem')
    // },
    async updateItemBtn(orders) {
      // console.log('扩展案例,自己实现 只需要修改 标题/卖点/价格/数量')
      this.updateDialogVisible = true
      // 动态获取想要的数据
      // 方式1：根据id发起ajax请求，获取最新的数据
      const {data: result} = await this.$http.get('/orders/' + orders.id)
      if (result.status !== 200) return this.$message.error('查询失败')
      this.updateOrdersForm.loginName=result.data.loginName;
      this.updateOrdersForm.userAddress=result.data.userAddress;
      this.updateOrdersForm.id=result.data.id;
      console.log(this.updateOrdersForm)
      // 方式2：从页面中获取数据（不推荐，数据不是最新的！）
      // this.updateUserForm = user;
    }
  }
}
</script>
<!-- 防止组件样式冲突 -->
<style lang="less" scoped>

</style>
